var crypto = require('../utils/crypto');
var express = require('express');
var db = require('../utils/admindb');
var http = require("../utils/http");

var app = express();

var configs = require(process.argv[2]);

db.init(configs.mysql());
app.listen(configs.port);

function send(res,ret){
	var str = JSON.stringify(ret);
	res.send(str)
}

//设置跨域访问
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

app.get('/login',function(req,res){
	var account = req.query.account;
	var password = req.query.password;
	db.check_account(account,password,function(data){
		if(data){
			var token = crypto.md5(account + password + Date.now());
			db.update_token(account,token,function(ret){
				if(ret){
					db.get_user_game_info(data.game_userid,function(userdata){
						if(userdata){
							http.send(res,0,"ok",{
								privilege_level:data.privilege_level,
								gems:userdata.gems,
								name:userdata.name,
								//headimg:userdata.headimg,
								phonenumber:account,
								gameid:data.game_userid,
								token:token
							});
						}
						else{
							http.send(res,3,"get game info failed.");	
						}
					});							
				}
				else{
					http.send(res,2,"failed to get token.");		
				}
			});
		}
		else{
			http.send(res,{errcode:1,errmsg:"login failed."});	
		}
	});
});

app.get('/search_user',function(req,res){
	var token = req.query.token;
	var userid = req.query.userid;
	if(token == null || token == "" || userid == null || userid == ""){
		http.send(res,-1,"unknown error.");
		return;
	}
	
	db.get_admin_by_token(token,function(ret){
		if(ret){
			db.get_user_game_info(userid,function(userdata){
				if(userdata){
					http.send(res,0,"ok",{
						userid:userdata.userid,
						gems:userdata.gems,
						name:userdata.name,
					});
				}
				else{
					http.send(res,2,"can't find user.");		
				}
			});
		}
		else{
			http.send(res,12580,"token check failed.");
		}
	});
});

app.get('/assign_gems',function(req,res){
	var token = req.query.token;
	var userid = req.query.userid;
	var num = req.query.num;
	
	if(token == null || token == "" || userid == null || userid == "" || num == null || num == ""){
		http.send(res,-1,"unknown error.");
		return;
	}
	
	num = parseInt(num);
	
	db.get_admin_by_token(token,function(ret){
		if(ret){
			db.get_user_game_info(userid,function(userdata){
				if(userdata){
					db.get_user_game_info(ret.game_userid,function(mydata){
						if(mydata){
							if(mydata.gems < num){
								http.send(res,4,"lack of gems",{gems:mydata.gems});	
							}
							else{
								db.dec_gems(mydata.userid,num);
								db.add_gems(userdata.userid,num);
								http.send(res,0,"ok",{gems:mydata.gems - num,targetgems:userdata.gems + num});
							}
						}
						else{
							http.send(res,3,"can't find user.");		
						}
					});
				}
				else{
					http.send(res,2,"can't find user.");		
				}
			});
		}
		else{
			http.send(res,12580,"token check failed.");
		}
	});
});

console.log("admin server is listening on port " + configs.port);